<template>
    <div class="mall-order">
        <van-nav-bar :title="$t('mall.title_four')" left-arrow fixed @click-left="_return" :z-index="666"></van-nav-bar>

        <ul class="mall-order-list">
            <li class="mall-order-item" v-for="item,index in orderList" :key="index">
                <div class="mall-order-item-cover">
                    <img :src="item.web + item.image" alt="">
                </div>
                <div class="mall-order-item-desc">
                    <h4 class="mall-order-item-title">{{item.title}}</h4>
                    <div class="mall-order-item-info">
                        <div class="mall-order-item-tip">
                            <span>{{item.time}}</span>
                            <p class="mall-order-item-price">{{Number(item.pay_price).toFixed(4)}}{{item.coin_name.toUpperCase()}}</p>
                        </div>
                    </div>
                </div>
            </li>
        </ul>
    </div>
</template>

<script>
    import moment from 'moment'
    export default {
        data () {
            return {
                orderList: [],
                listNum:'',
                page:1
            }
        },
        methods: {
            getData () {
                this.$api.get('goods/GoodsOrder',{page:this.page})
                    .then(res =>{
                        if(res.detail.list.length < 10){
                            window.removeEventListener("scroll", this.reachBottom,true)
                        }
                        res.detail.list.forEach(item => this.orderList.push(item))
                        this.orderList.forEach(i => this.$set(i,'time',moment(i.create_time*1000).format('YYYY.MM.DD')))
                    })
            },
            _return(){
                window.removeEventListener("scroll", this.reachBottom,true)
                this.$emit('_return')
            },
            getScrollTop () {
                // 考虑到浏览器版本兼容性问题，解析方式可能会不一样
                return document.documentElement.scrollTop || document.body.scrollTop
            },
            getWinHeight () {
                // 浏览器可见内容高度 || 浏览器所有内容高度(考虑到浏览器版本兼容性问题，解析方式可能会不一样)
                return document.documentElement.clientHeight || document.body.clientHeight
            },
            getScrollHeight() {
                let bodyScrollHeight = 0
                let documentScrollHeight = 0
                if (document.body) {
                    bodyScrollHeight = document.body.scrollHeight
                }
                if (document.documentElement) {
                    documentScrollHeight = document.documentElement.scrollHeight
                }
                // 当页面内容超出浏览器可视窗口大小时，Html的高度包含body高度+margin+padding+border所以html高度可能会大于body高度
                return (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight
            },
            isReachBottom () {
                const scrollTop = this.getScrollTop() // 获取滚动条的高度
                const winHeight = this.getWinHeight() // 一屏的高度
                const scrollHeight = this.getScrollHeight() // 获取文档总高度
                return scrollTop >= parseInt(scrollHeight) - winHeight
            },
            reachBottom() {
                if (this.isReachBottom()) {
                    if (this.listNum > 10) {
                        this.page ++
                        this.getData()
                    } else {
                        window.onscroll = null
                    }
                }
            },
        },
        created(){
            this.getData()
        },
        mounted() {
            window.addEventListener("scroll", this.reachBottom,true)
        },
    }
</script>

<style scoped lang="stylus">
    [class*=van-hairline]::after{
        border: none;
    }
    .mall-order {
        padding: 56px 0;
        &-list {
            width 94%
            margin 0 auto
        }
        &-item {
            padding: 15px;
            display: flex;
            margin-bottom: 10px;
            background:linear-gradient(90deg,rgba(42,42,42,1) 0%,rgba(46,46,46,1) 100%);
            box-shadow:0px 6px 12px 0px rgba(0,0,0,0.5);
            border-radius 5px
            &-cover {
                width: 150px;
                flex: 0 0 150px;
                height: 95px;
                margin-right: 8px;
                img {
                    width: 100%;
                    height: 100%;
                }
            }
            &-title {
                font-size: 14px;
                color: #D8D8D8;
                line-height: 20px;
                overflow: hidden;
                text-overflow: ellipsis;
                display: -webkit-box;
                -webkit-line-clamp: 2;
                -webkit-box-orient: vertical;
            }
            &-desc {
                flex: 1;
                display: flex;
                flex-flow: column nowrap;
                justify-content: space-between;
            }
            &-tip {
                margin-top: 5px;
                span {
                    font-size 12px
                    color #898989
                }
            }
            &-price {
                font-size: 20px;
                color: #CBAF89;
            }
        }
    }
</style>
